{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.先导入需要的模块\n",
    "import requests\n",
    "\n",
    "# 2.输入我们api_secret、api_key\n",
    "api_secret = \"_DAnfXANFVF6sfJjxeo8KSBya2-qfWxk\"\n",
    "api_key = 'dOpMliQxn4WUJobcHsZw9715N7lsS3wN'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3.目标url\n",
    "# 这里也可以使用本地图片 例如：filepath =\"image/tupian.jpg\"\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'https://gitee.com/AliceZING/apiworkspace/raw/master/lzy.jpg'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#4.沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5.准备后面的数据\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    # 是否检测并返回根据人脸特征判断出的年龄、性别、情绪等属性。\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 6.requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1603528161,bf04685a-191e-4ce4-8633-ae5842c503d8\",\"time_used\":1330,\"faces\":[{\"face_token\":\"9a6ebaa4d2e0253c195f8db569d73fb9\",\"face_rectangle\":{\"top\":439,\"left\":444,\"width\":439,\"height\":439},\"attributes\":{\"gender\":{\"value\":\"Female\"},\"age\":{\"value\":20},\"smile\":{\"value\":99.837,\"threshold\":50.000},\"emotion\":{\"anger\":0.003,\"disgust\":0.001,\"fear\":0.001,\"happiness\":77.495,\"neutral\":0.010,\"sadness\":0.001,\"surprise\":22.488}}}],\"image_id\":\"PlmSgvTxhOxSDaCZGBbs0w==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603528161,bf04685a-191e-4ce4-8633-ae5842c503d8',\n",
       " 'time_used': 1330,\n",
       " 'faces': [{'face_token': '9a6ebaa4d2e0253c195f8db569d73fb9',\n",
       "   'face_rectangle': {'top': 439, 'left': 444, 'width': 439, 'height': 439},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 20},\n",
       "    'smile': {'value': 99.837, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.003,\n",
       "     'disgust': 0.001,\n",
       "     'fear': 0.001,\n",
       "     'happiness': 77.495,\n",
       "     'neutral': 0.01,\n",
       "     'sadness': 0.001,\n",
       "     'surprise': 22.488}}}],\n",
       " 'image_id': 'PlmSgvTxhOxSDaCZGBbs0w==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json() \n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '1c5f583e56c0475969026fc0516bc857',\n",
       " 'time_used': 143,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1603529415,21cdc2ab-b817-46d0-b218-ceb4ca86ae3b',\n",
       " 'outer_id': '2020',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "api_secret = \"_DAnfXANFVF6sfJjxeo8KSBya2-qfWxk\"\n",
    "api_key = 'dOpMliQxn4WUJobcHsZw9715N7lsS3wN'\n",
    "\n",
    "# 1.FaceSet Create\n",
    "import requests,json\n",
    "\n",
    "display_name = \"人脸集合2020\" #自定义人脸集合的名字\n",
    "outer_id = \"2020\" #自定义标识\n",
    "user_data = \"renlian\" #自定义用户信息\n",
    "\n",
    "CreateFace_Url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\" #调用URL\n",
    "payload = {\n",
    "    # 请求参数\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'display_name':display_name,\n",
    "    'outer_id':outer_id,\n",
    "    'user_data':user_data\n",
    "}\n",
    "r = requests.post(CreateFace_Url, params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '1c5f583e56c0475969026fc0516bc857',\n",
       " 'tags': '',\n",
       " 'time_used': 85,\n",
       " 'user_data': 'renlian',\n",
       " 'display_name': '人脸集合2020',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603529729,621711f9-2b4e-4424-a342-6c1fe2300e42',\n",
       " 'outer_id': '2020'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.FaceSet GetDetail（获取人脸集合信息）\n",
    "GetDetail_Url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'outer_id':outer_id,\n",
    "}\n",
    "r = requests.post(GetDetail_Url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '1c5f583e56c0475969026fc0516bc857',\n",
       " 'time_used': 630,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1603529731,c6cddd63-15e5-4166-b145-6050ffe5eba2',\n",
       " 'outer_id': '2020',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3.FaceSet AddFace（增加人脸信息）\n",
    "AddFace_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'1c5f583e56c0475969026fc0516bc857',\n",
    "    'face_tokens':'9a6ebaa4d2e0253c195f8db569d73fb9', # qianmiande\n",
    "}\n",
    "r = requests.post(AddFace_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '1c5f583e56c0475969026fc0516bc857',\n",
       " 'face_removed': 1,\n",
       " 'time_used': 172,\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603529796,890511a5-e5d4-4b40-aa6d-400c9f0c698b',\n",
       " 'outer_id': '2020',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4.FaceSet RemoveFace（移除人脸信息）\n",
    "RemoveFace_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/removeface\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'1c5f583e56c0475969026fc0516bc857',\n",
    "    'face_tokens':'9a6ebaa4d2e0253c195f8db569d73fb9',\n",
    "}\n",
    "r = requests.post(RemoveFace_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '1c5f583e56c0475969026fc0516bc857',\n",
       " 'request_id': '1603530460,2d58e296-985a-4601-a3a7-7eb0b8c0505b',\n",
       " 'time_used': 80,\n",
       " 'outer_id': '2020'}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 5.FaceSet Update（更新人脸集合信息）\n",
    "Update_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'1c5f583e56c0475969026fc0516bc857',\n",
    "    'user_data':\"renlian\",\n",
    "}\n",
    "r = requests.post(Update_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faces1': [{'face_rectangle': {'width': 304,\n",
       "    'top': 167,\n",
       "    'left': 171,\n",
       "    'height': 304},\n",
       "   'face_token': 'd65ff8d654813ae7dfdb23b88faaccef'}],\n",
       " 'faces2': [{'face_rectangle': {'width': 142,\n",
       "    'top': 266,\n",
       "    'left': 256,\n",
       "    'height': 142},\n",
       "   'face_token': '3aa9f2511c29a8cff068c73b8299cb60'},\n",
       "  {'face_rectangle': {'width': 83, 'top': 595, 'left': 298, 'height': 83},\n",
       "   'face_token': '7f623eb0072627164cfa61b510a1ad9c'}],\n",
       " 'time_used': 702,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'confidence': 42.191,\n",
       " 'image_id2': 'yIh/tAI0SYhxuFBfE3+EQA==',\n",
       " 'image_id1': 'ACWk+vVP6trmH7sljHsfgA==',\n",
       " 'request_id': '1603530787,4960705a-965b-4d6b-9d1f-3ad3d72c6050'}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 6.Compare Face（对比人脸相似度）\n",
    "tay = \"https://wx3.sinaimg.cn/mw690/ed4d61ably1ge90p0nsoqj20u011i439.jpg\"\n",
    "#liudehua02 = \"https://tse3-mm.cn.bing.net/th/id/OIP.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=Api&rs=1\"\n",
    "lauv = \"https://wx2.sinaimg.cn/mw690/006UkIMQgy1gilovpedhzj30u011ijv3.jpg\"\n",
    "\n",
    "Compare_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "payload ={\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'image_url1':tay,\n",
    "    'image_url2':lauv,\n",
    "}\n",
    "r = requests.post(Compare_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
